<template>
  <div>
    <div class="cart-bottom bgf">
      <span style="display:inline-block;width:50%;text-align:right;line-height:40px">
        合计:
        <span class="colc">￥{{toal.amount}}</span>
      </span>

      <button
        style="border-radius:0;height:40px;width:100px;border:none;background:rgb(0, 178, 163);color:#fff;float:right"
        @click="orderCarAdd"
      >支付</button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    toal: Object
  },
  data() {
    return {
      ts: this.newStore(),
      prepayid: {}
    }
  },
  methods: {
    //支付
    orderCarAdd() {
      const res = {
        token: localStorage.getItem('token'),
        carid: this.$route.query.carid,
        mailtype: '0',
        addressee: '李文勇1',
        tel: '15602018794',
        address: '天津市南开区',
        ordertoken: this.toal.ordertoken,
        orderno: this.toal.orderno
      }
      this.spost(this.ts, '/Orderc/orderCarAdd', res).then(ts => {
        if (ts.d.flag = '200') {
          this.prepayid = ts.d.data
          this.PayMoney()
        }
      })
    },
    //立即支付调用微信接口，生产环境使用
    PayMoney() {
      if (typeof WeixinJSBridge == 'undefined') {
        if (document.addEventListener) {
          document.addEventListener(
            'WeixinJSBridgeReady',
            this.onBridgeReady,
            false
          )
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady)
          document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady)
        }
      } else {
        this.onBridgeReady()
      }
    },
    onBridgeReady() {
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
          timeStamp: `${this.prepayid.timeStamp}`, // 时间戳，自1970年以来的秒数
          appId: `${this.prepayid.appId}`, // 公众号名称，由商户传入
          nonceStr: `${this.prepayid.nonceStr}`, // 随机串
          package: `${this.prepayid.package}`, //
          signType: `${this.prepayid.signType}`, // 微信签名方式
          paySign: `${this.prepayid.paySign}` // 微信签名
        },
        res => {
          // alert(JSON.stringify(res))
          // alert(`输入密码后从微信服务器返回来的支付结果：${JSON.stringify(res)}`);
          if (res.err_msg == 'get_brand_wcpay_request:ok') {
            console.log('res.err_msg', res.err_msg)
            alert('6666666666')
            // this.$router.push({ name: 'eporder' })
            // this.$router.push({
            //   name: 'chufang-payOff',
            //   query: {
            //     je_sf: this.je_sf,
            //     payResult: true,
            //     payMessage: '支付成功'
            //   }
            // })
          } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
            // this.$router.push({ name: 'payOff', query: { je_sf: this.je_sf, payResult: false, payMessage: '取消支付' } });
            // this.showAlert = true
            // this.alertText = '已取消支付'
            console.log('res.err_msg', res.err_msg)
          } else {
            alert('0900000')
            // this.$router.push({ name: 'payOff', query: { je_sf: this.je_sf, payResult: false, payMessage: res.err_msg } });
            // this.showAlert = true
            // this.alertText = `支付失败,错误信息${res.err_msg}`
            console.log('res.err_msg', res.err_msg)
          }
        }
      )
    }
  }
}
</script>

<style>
</style>
